package com.group7.mall.controller;

import com.group7.mall.bean.Result;
import com.group7.mall.dao.TbNetProfitDao;
import com.group7.mall.dto.*;
import com.group7.mall.entity.TbNetProfit;
import com.group7.mall.entity.TbSeller;
import com.group7.mall.entity.TbUser;
import com.group7.mall.entity.TbUserOrder;
import com.group7.mall.req.SellerNetProfitRequest;
import com.group7.mall.req.SellerOrderPageRequest;
import com.group7.mall.resp.SellerOrderPageResponse;
import com.group7.mall.service.SellerOrderService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("seller-order-controller")
public class SellerOrderController {

    @Resource
    private SellerOrderService sellerOrderService;

    @Resource
    private TbNetProfitDao tbNetProfitDao;

    @PostMapping("show-net-profit")
    public Result<Double> showNetProfit(@RequestBody SellerNetProfitRequest sellerNetProfitRequest) {
        List<TbNetProfit> tbNetProfits = tbNetProfitDao.queryByReq(sellerNetProfitRequest);
        Double profit = 0.0;
        if (tbNetProfits != null) {
            for(TbNetProfit x: tbNetProfits) {
                profit += x.getValue();
            }
        }
        System.out.println(profit + " 10086");
        return Result.ok(profit);
    }


    @PostMapping("show-order-page")
    public Result<SellerOrderPageResponse<SellerOrderPreviewDto>> showOrderByPage(@RequestBody SellerOrderPageRequest sellerOrderPageRequest) {
//        SellerOrderPageResponse<SellerOrderPreviewDto> sellerOrderPageResponse = new SellerOrderPageResponse<SellerOrderPreviewDto>();
//        sellerOrderPageResponse.setRecords(sellerOrderService.showOrderByPage(sellerOrderPageRequest));
        return Result.ok(sellerOrderService.showOrderByPage(sellerOrderPageRequest));
    }

    @PostMapping("show-order-detail")
    public Result<SellerOrderDetailDto> showOrderDetail(@RequestBody SellerOrderDetailDto sellerOrderDetailDto) {
        return Result.ok(sellerOrderService.showOrderDetail(sellerOrderDetailDto.getOrderId()));
    }

    @PostMapping("delivery")
    public Result<Integer> delivery(@RequestBody TbUserOrder tbUserOrder) {
        return Result.ok(sellerOrderService.delivery(tbUserOrder));
    }

    @PostMapping("update-express-number")
    public Result<Integer> updateExpressNumber(@RequestBody TbUserOrder tbUserOrder) {
        return Result.ok(sellerOrderService.delivery(tbUserOrder));
    }

    @PostMapping("service-finish")
    public Result<Integer> serviceFinish(@RequestBody TbUserOrder tbUserOrder) {
        return Result.ok(sellerOrderService.serviceFinish(tbUserOrder));
    }

    @PostMapping("show-order-count")
    public Result<List<Integer>> showOrderCount(@RequestBody TbSeller tbSeller) {
        return Result.ok(sellerOrderService.showOrderCount(tbSeller.getStoreId()));
    }

    @PostMapping("show-order-price")
    public Result<List<Double>> showOrderPrice(@RequestBody TbSeller tbSeller) {
        return Result.ok(sellerOrderService.showOrderPrice(tbSeller));
    }
}
